1694D - Fake Plastic Trees - CodeForces Solution


dfs and similar dp greedy trees *1700

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#define ll long long int
#define pb push_back
#define ff first
#define ss second
#define rapid ios_base:: sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define ld long double
#define tc ll test; cin>>test; while(test--)
using namespace std;

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

const int N=2e5+10;
vector<ll> adj[N];
vector<pair<ll,ll>> vpp(N);
ll n;
ll ans;
ll DFS(ll v){
    ll sum =0;
    for(ll u:adj[v]){
        sum+= DFS(u);
    }

    if(sum< (vpp[v].first)){
        ++ans;
        return vpp[v].second;
    }

    return min(vpp[v].second,sum);
}

void solve(){
    cin>>n;

    ll par;
    for(ll i =2;i<=n;i++){
        cin>>par;
        adj[par].pb(i);
    }
    for(ll i=1;i<=n;i++){
        cin>>vpp[i].first>>vpp[i].second;
    }
    ans =0;
    DFS(1);
    cout<<ans<<endl;

    for(ll i =1;i<=n;i++){
        adj[i].clear();
    }

}


int main() {
	// your code goes here
    // rapid;
    ll test;
	cin>>test;


	for(ll i =0;i<test;i++){
		solve();
	}
    
    	    
	return 0;
}


Comments

Submit
0 Comments
More Questions

145. Binary Tree Postorder Traversal
94. Binary Tree Inorder Traversal
101. Symmetric Tree
77. Combinations
46. Permutations
226. Invert Binary Tree
112. Path Sum
1556A - A Variety of Operations
136. Single Number
169. Majority Element
119. Pascal's Triangle II
409. Longest Palindrome
1574A - Regular Bracket Sequences
1574B - Combinatorics Homework
1567A - Domino Disaster
1593A - Elections
1607A - Linear Keyboard
EQUALCOIN Equal Coins
XOREQN Xor Equation
MAKEPAL Weird Palindrome Making
HILLSEQ Hill Sequence
MAXBRIDGE Maximise the bridges
WLDRPL Wildcard Replacement
1221. Split a String in Balanced Strings
1002. Find Common Characters
1602A - Two Subsequences
1555A - PizzaForces
1607B - Odd Grasshopper
1084A - The Fair Nut and Elevator
1440B - Sum of Medians